package org.test.udp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadTxt {
	   /**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
    public static String txtToString(String filePath) throws IOException{
    	InputStreamReader isr;   
            isr = new InputStreamReader(new FileInputStream("D:/test.txt"), "gbk");   
             BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream("D:/test.txt"), "gbk"));   
               String s=null;   
               StringBuffer result = new StringBuffer("");
               while((s=read.readLine())!=null)   
               {  
                   if(s.trim().length()>1){   
                	   result.append(s.trim());
                	   result.append("\r\n");
                   }   
               }
               //result.delete(result.length()-2, result.length());
               return result.toString();
    }

    public static void main(String[] args) throws IOException{
        String result = txtToString("D:/test.txt");
        System.out.println(result);
    }

}
